<?php

namespace Core;

/**
 * Description of Controller
 *
 * @author lxc
 */
class Controller
{

	public $assign = [];
	public $outPut = '';
	public $outPut_type = 'text/html';
	protected $request = null;

	public function __construct()
	{

	}

	public function _set_request($request)
	{
		$this->request = $request;
	}

	/**
	 * _get
	 * @param type $key
	 * @return type
	 */
	public function _get($key)
	{
		$get = $this->request->get('get');
		return isset($get[$key]) ? $get[$key] : null;
	}

	/**
	 * _post
	 * @param type $key
	 * @return type
	 */
	public function _post($key)
	{
		$post = $this->request->get('post');
		return isset($post[$key]) ? $post[$key] : null;
	}

	/**
	 * _echo
	 * @param type $string
	 * @return type
	 */
	public function _echo($string)
	{
		$this->outPut .= $string;
		return $this->outPut;
	}

	/**
	 * 显示模板
	 * @param string $template
	 */
	public function _display($template = null)
	{
		$backtrace = debug_backtrace();
		$this->assign['_object_'] = $backtrace[1]['object'];
		//默认将调用它的类的名称当做模板目录
		$dir_default = trim($backtrace[1]['class'], 'App\\Controller\\');
		if ($template == '' || is_null($template))
			$template = $dir_default . '/' . $backtrace[1]['function'];
		else {
			if (strpos($template, '/') === false)
				$template = $dir_default . '/' . $template;
		}
		$template_file = APP_PATH . '/App/View/' . $template . '.html';
		if (!is_file($template_file))
			$this->_error('页面错误', 502);
		else {
			extract($this->assign);
			ob_start();
			include $template_file;
			$this->outPut .= ob_get_clean();
		}
		return $this->outPut;
	}

	protected function contentType($type = 'text/html')
	{
		$this->outPut_type = $type;
		return $this;
	}

	/**
	 * 模板赋值
	 * @param type $name
	 * @param type $value
	 * @return $this
	 */
	public function _assign($name, $value)
	{
		$this->assign[$name] = $value;
		return $this;
	}

	/**
	 * 错误页面
	 * @param type $content
	 */
	public function _error($content = '', $code = 1, $url = 'javascript:history.back(-1);')
	{
		$waitSecond = 3;
		if ($url == '')
			$url = 'javascript:history.back(-1);';
		$this->outPut = <<<EOF
<html>
	<head>
		<meta charset="utf-8" />
		<title>Error</title>
	</head>
	<body>
		<div>$content</div>
		<p class="jump">
		页面自动 <a id="href" href="$url">跳转</a> 等待时间： <b id="wait">$waitSecond</b>
		</p>
		<script type="text/javascript">
			(function(){
				var wait = document.getElementById('wait'),href = document.getElementById('href').href;
				var interval = setInterval(function(){
					var time = --wait.innerHTML;
					if(time <= 0) {
						location.href = href;
						clearInterval(interval);
					};
				}, 1000);
			})();
		</script>
	</body>
</html>
EOF;
	}

	public function _success($content = '', $code = 0, $url = '/')
	{
		$waitSecond = 3;
		if ($url == '')
			$url = '/';
		$this->outPut = <<<EOF
<html>
	<head>
		<meta charset="utf-8" />
		<title>Error</title>
	</head>
	<body>
		<div>$content</div>
		<p class="jump">
		页面自动 <a id="href" href="$url">跳转</a> 等待时间： <b id="wait">$waitSecond</b>
		</p>
		<script type="text/javascript">
			(function(){
				var wait = document.getElementById('wait'),href = document.getElementById('href').href;
				var interval = setInterval(function(){
					var time = --wait.innerHTML;
					if(time <= 0) {
						location.href = href;
						clearInterval(interval);
					};
				}, 1000);
			})();
		</script>
	</body>
</html>
EOF;
	}

	public function __call($name, $arguments)
	{
		$backtrace = debug_backtrace();
		$class = $backtrace[1]['class'];
		$this->outPut = self::_empty($class, $name);
	}

	/**
	 * 空的类或方法
	 * @param type $class
	 * @param type $action
	 */
	public static function _empty($class, $action = null)
	{
		if (is_null($action))
			$string = 'Class "' . $class . '" is not fount.';
		else
			$string = 'Action "' . $action . '" is not fount in Class "' . $class . '".';
		$len = strlen($string);
		$response = <<<EOF
HTTP/1.1 404 File not fount
Server: FK-Blog-1.0
Content-Type: text/html;charset=utf-8
Connection: keep-alive
Content-Length: $len

$string
EOF;
		return $response;
	}

	public static function log($file_name = 'run.log', $user_data = [], $tag = 'INFO')
	{

	}

}
